﻿
var Linkage = {
    option:{},
    init: function (option) {
        if(Linkage.option.level>3){
            alert("当前只支持3级与2级联动");
            return false;
        }
        Linkage.option = option;
            option.elms[0].$elm.attr("placeHolder", "请选择" + option.elms[0].text).select({
                multi:Linkage.option.elms[0].multi?Linkage.option.elms[0].multi:false,
                title: "请选择" + option.elms[0].text,
                items: Linkage.getLevel1()
            });
        option.elms[1].$elm.attr("placeHolder", "请选择" + option.elms[1].text).select({
            multi:Linkage.option.elms[1].multi?Linkage.option.elms[1].multi:false,
            title: "请选择" + option.elms[1].text,
            items: Linkage.getLevel2()
        });
        option.elms[0].$elm.change(function () {
            option.elms[1].$elm.val("").select("update", { items:  Linkage.getLevel2() });
            option.elms[2].$elm.val("").select("update", { items: [] });
        })
        if(Linkage.option.level>=3){

            option.elms[2].$elm.attr("placeHolder", "请选择" + option.elms[2].text).select({
                multi:Linkage.option.elms[2].multi?Linkage.option.elms[2].multi:false,
                title: "请选择" + option.elms[2].text,
                items: Linkage.getLevel3()
            });
            option.elms[1].$elm.change(function () {
                option.elms[2].$elm.val("").select("update", { items: Linkage.getLevel3() });
            })
        }





    },
    getLevel1 : function () {
        var p = [];
        for (var i = 0; i < Linkage.option.linkageData.length; i++) {
            p.push({title:Linkage.option.linkageData[i].name,value:Linkage.option.linkageData[i].value});
        }
        return p;
    },
    getLevel2: function (p) {
        p = Linkage.option.elms[0].$elm.val();
        for (var i = 0; i < Linkage.option.linkageData.length; i++) {
            if (Linkage.option.linkageData[i].code === p || Linkage.option.linkageData[i].name === p) {
                c = [];
                $.each(Linkage.option.linkageData[i].sub, function () {
                    c.push({title:this.name,value:this.value})
                })
                return c;
            }
        }
        return [];
    },
    getLevel3: function () {
        p = Linkage.option.elms[0].$elm.val();
        c = Linkage.option.elms[1].$elm.val();
        for (var i = 0; i < Linkage.option.linkageData.length; i++) {
            if (Linkage.option.linkageData[i].code === p || Linkage.option.linkageData[i].name === p) {
                for (var j = 0; j < Linkage.option.linkageData[i].sub.length; j++) {
                    if (Linkage.option.linkageData[i].sub[j].code === c || Linkage.option.linkageData[i].sub[j].name === c) {
                        a = [];
                        $.each(Linkage.option.linkageData[i].sub[j].sub, function () {
                            a.push({title:this.name,value:this.value})
                        })
                        return a;
                    }
                }
            }
        }
        return [];
    }
}